package com.ruoyi.experiment.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.experiment.domain.CtfExperimentalEnvironmentExample;
import com.ruoyi.experiment.domain.TeachingExperimentalEnvironment;
import com.ruoyi.experiment.domain.TeachingExperimentalEnvironmentExample;
import com.ruoyi.experiment.mapper.CtfExperimentalEnvironmentExampleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.experiment.mapper.CtfExperimentalEnvironmentMapper;
import com.ruoyi.experiment.domain.CtfExperimentalEnvironment;
import com.ruoyi.experiment.service.ICtfExperimentalEnvironmentService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * CTF实验环境Service业务层处理
 * 
 * @author 邓鑫
 * @date 2023-07-29
 */
@Service
public class CtfExperimentalEnvironmentServiceImpl implements ICtfExperimentalEnvironmentService 
{
    @Resource
    private CtfExperimentalEnvironmentMapper ctfExperimentalEnvironmentMapper;
    @Resource
    private CtfExperimentalEnvironmentExampleMapper ctfExperimentalEnvironmentExampleMapper;

    /**
     * 查询CTF实验环境
     * 
     * @param ctfEnvironmentId CTF实验环境主键
     * @return CTF实验环境
     */
    @Override
    public CtfExperimentalEnvironment selectCtfExperimentalEnvironmentByCtfEnvironmentId(Long ctfEnvironmentId)
    {
        return ctfExperimentalEnvironmentMapper.selectCtfExperimentalEnvironmentByCtfEnvironmentId(ctfEnvironmentId);
    }

    /**
     * 查询CTF实验环境列表
     * 
     * @param ctfExperimentalEnvironment CTF实验环境
     * @return CTF实验环境
     */
    @Override
    public List<CtfExperimentalEnvironment> selectCtfExperimentalEnvironmentList(CtfExperimentalEnvironment ctfExperimentalEnvironment)
    {
        return ctfExperimentalEnvironmentMapper.selectCtfExperimentalEnvironmentList(ctfExperimentalEnvironment);
    }

    /**
     * 新增CTF实验环境
     * 
     * @param ctfExperimentalEnvironment CTF实验环境
     * @return 结果
     */
    @Override
    public int insertCtfExperimentalEnvironment(CtfExperimentalEnvironment ctfExperimentalEnvironment)
    {
        return ctfExperimentalEnvironmentMapper.insertCtfExperimentalEnvironment(ctfExperimentalEnvironment);
    }

    /**
     * 修改CTF实验环境
     * 
     * @param ctfExperimentalEnvironment CTF实验环境
     * @return 结果
     */
    @Override
    public int updateCtfExperimentalEnvironment(CtfExperimentalEnvironment ctfExperimentalEnvironment)
    {
        return ctfExperimentalEnvironmentMapper.updateCtfExperimentalEnvironment(ctfExperimentalEnvironment);
    }

    /**
     * 批量删除CTF实验环境
     * 
     * @param ctfEnvironmentIds 需要删除的CTF实验环境主键
     * @return 结果
     */
    @Override
    public int deleteCtfExperimentalEnvironmentByCtfEnvironmentIds(Long[] ctfEnvironmentIds)
    {
        return ctfExperimentalEnvironmentMapper.deleteCtfExperimentalEnvironmentByCtfEnvironmentIds(ctfEnvironmentIds);
    }

    /**
     * 删除CTF实验环境信息
     * 
     * @param ctfEnvironmentId CTF实验环境主键
     * @return 结果
     */
    @Override
    public int deleteCtfExperimentalEnvironmentByCtfEnvironmentId(Long ctfEnvironmentId)
    {
        return ctfExperimentalEnvironmentMapper.deleteCtfExperimentalEnvironmentByCtfEnvironmentId(ctfEnvironmentId);
    }

    /**
     * 生成环境实例
     */
    @Override
    @Transactional
    public void addExample(Long ctfEnvironmentId) {
        //获取环境对象
        CtfExperimentalEnvironment cee =ctfExperimentalEnvironmentMapper.selectCtfExperimentalEnvironmentByCtfEnvironmentId(ctfEnvironmentId);
        CtfExperimentalEnvironmentExample environmentExample=new CtfExperimentalEnvironmentExample();
        environmentExample.setCtfEnvironmentId(ctfEnvironmentId);
        environmentExample.setExampleCreateTime(LocalDateTime.now()); //设置创建时间
        environmentExample.setExampleEndTime(LocalDateTime.now().plusHours(cee.getCtfEnvironmentTime())); //设置结束时间
        ctfExperimentalEnvironmentExampleMapper.insertCtfExperimentalEnvironmentExample(environmentExample);
    }
}
